package com.topisv.tms.web;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Maps;
import com.topisv.tms.entity.Customers;
import com.topisv.tms.entity.JSONParam;
import com.topisv.tms.entity.OrdenoSource;
import com.topisv.tms.entity.QueryParamHolder;
import com.topisv.tms.entity.ShopSource;
import com.topisv.tms.service.CustomersService;
import com.topisv.tms.service.LogisticsCompanyService;
import com.topisv.tms.service.OrdenoSourceService;
import com.topisv.tms.service.ShopSourceService;
import com.topisv.tms.web.base.BaseDataTableController;

@Controller
@RequestMapping(value = "/v2/shopSource")
public class ShopSourceController extends BaseDataTableController{

	@Autowired
	private ShopSourceService shopSourceService;
	@Autowired
	private CustomersService customersService;
	@Autowired
	private OrdenoSourceService ordenoSourceService;
	@Autowired
	private LogisticsCompanyService logisticsCompanyService;

	private static Map<String, String> sortTypes = Maps.newLinkedHashMap();
	static {
		sortTypes.put("auto", "自动");
		sortTypes.put("shopName", "名称");
	}

	@RequestMapping(value = "v2_show")
	public String list() {
		return "shopSource/shopSourceList";
	}
	
	@RequestMapping(value = "page")
	public void page(@RequestBody JSONParam[] paramJson, HttpServletRequest request,HttpServletResponse response){
		try {
			// 客户端参数
			QueryParamHolder paramHolder = collectParam(paramJson);
			Page<ShopSource> page = shopSourceService.pageShopSourcePage(paramHolder);
			List<ShopSource> shopSources = page.getContent();
			for (ShopSource o : shopSources) {
				setShopSource(o);
			}
			// 封装查询结果
			response.getWriter().print(bulidJSON(page));
		} catch (Exception e) {
			
		}
	}
	
	private void setShopSource(ShopSource o){
		o.setCustomers(customersService.findByCustomerCode(o.getCustomerCode()));
		o.setOrdenoSource(ordenoSourceService.findByOrdenoSourceCode(o.getOrdenoSourceCode()));
	}

	@RequestMapping(value = "create", method = RequestMethod.GET)
	public String createForm(Model model) {
		model.addAttribute("shopSource", new ShopSource());
		model.addAttribute("action", "create");
		model.addAttribute("customers", customersService.findAll());
		model.addAttribute("ordenoSource", ordenoSourceService.findAll());
		model.addAttribute("companys", logisticsCompanyService.findAll());
		
		model.addAttribute("function", "增加店铺信息");
		return "shopSource/shopSourceForm";
	}

	@RequestMapping(value = "create", method = RequestMethod.POST)
	public String create(
			@Valid ShopSource shopSource, RedirectAttributes redirectAttributes) {
		try {
			OrdenoSource os=ordenoSourceService.findByOrdenoSourceCode(shopSource.getOrdenoSourceCode());
			Customers ct=customersService.findByCustomerCode(shopSource.getCustomerCode());
			String shopCode=getShopCode(ct.getCustomerCode(),os.getOrdenoSourceCode());
			shopSource.setCreateDate(new Date());
			shopSource.setCustomerCode(ct.getCustomerCode());
			shopSource.setShopCode(shopCode);
			shopSourceService.save(shopSource);
			redirectAttributes.addFlashAttribute("message", "创建成功");
		} catch (Exception e) {
			redirectAttributes.addFlashAttribute("message", e.getMessage());
		}
		return "redirect:/v2/shopSource/v2_show";
	}
	
	private String getShopCode(String customerCode,String orderSourceCode){
		return customerCode+"_"+orderSourceCode+"_"+RandomStringUtils.randomNumeric(6);
	}

	@RequestMapping(value = "update/{id}", method = RequestMethod.GET)
	public String updateForm(@PathVariable("id") Long id, Model model) {
		try {
			ShopSource obj=shopSourceService.findOne(id);
			model.addAttribute("customers", customersService.findAll());
			model.addAttribute("ordenoSource", ordenoSourceService.findAll());
			model.addAttribute("companys", logisticsCompanyService.findAll());
			model.addAttribute("shopSource", obj);
			model.addAttribute("action", "update");
			model.addAttribute("status", "1");
		} catch (Exception e) {
			model.addAttribute("status", "0");
			model.addAttribute("message", e.getMessage());
		}
		model.addAttribute("function", "更新店铺信息");
		return "shopSource/shopSourceForm";
	}

	@RequestMapping(value = "update", method = RequestMethod.POST)
	public String update(
			@Valid @ModelAttribute("preloadShopSource") ShopSource shopSource,
			RedirectAttributes redirectAttributes) {
		try {
			shopSource.setCreateDate(new Date());
			shopSourceService.save(shopSource);
			redirectAttributes.addFlashAttribute("message", "更新成功");

		} catch (Exception e) {
			redirectAttributes.addFlashAttribute("message", e.getMessage());

		}
		return "redirect:/v2/shopSource/v2_show";
	}

	@RequestMapping(value = "delete/{id}")
	public String delete(@PathVariable("id") Long id,
			RedirectAttributes redirectAttributes) {
		try {
			shopSourceService.delete(id);
			redirectAttributes.addFlashAttribute("message", "删除成功");
		} catch (Exception e) {
			redirectAttributes.addFlashAttribute("message", e.getMessage());
		}
		return "redirect:/v2/shopSource/v2_show";
	}

}
